/*
 * @Author: zipeng 1134348223@qq.com
 * @Date: 2024-11-05 22:44:35
 * @LastEditors: zipeng 1134348223@qq.com
 * @LastEditTime: 2025-10-27 21:56:55
 * @FilePath: \myfifo\Timer.c
 * @Description: 
 * 
 * Copyright (c) 2024 by HuaWeiTecho, All Rights Reserved. 
 */
#include "Handler.h"

typedef struct MyStruct
{
    void (*handler)(void);
    uint32_t repeatTime;
} TimerStru;

static TimerStru g_TimerStru[REGIST_THRESHOLD];
static uint32_t g_timerRegistNum = 0; //注册的定时器个数

static void TimInit(uint32_t irqTime){
    // bsp hard 定义外部MCU硬件定时器函数
}

/* 定时器驱动内核 */
static void HandlerRun(void) {
    for (uint32_t i = 0; i < REGIST_THRESHOLD; i++) {
        if (g_TimerStru[i].handler != NULL) {
            g_TimerStru[i].handler();
            g_TimerStru[i].repeatTime--;
            if (g_TimerStru[i].repeatTime <= 0 && g_TimerStru[i].repeatTime != 0XFF) {
                g_TimerStru[i].handler = NULL;
            }
        }
    }
}
// todo 分几种定时执行时间 1ms 100ms
/*
* 万能式定时器注册函数
*/
void HandlerFunRegist(void (*handler)(void), uint32_t repeatTime)
{
    if (handler == NULL || g_timerRegistNum >= REGIST_THRESHOLD) {
        return;
    }
    static uint8_t initFlag = 0x55;
    if (initFlag == 0x55) {
        TimInit(10); //未定义硬件先定义硬件 0ms驱动
        memset((uint8_t *)g_TimerStru, 0, sizeof(g_TimerStru));
        initFlag = 0xaa;
    }
    g_TimerStru[g_timerRegistNum].handler = handler;
    g_TimerStru[g_timerRegistNum].repeatTime = repeatTime;
    g_timerRegistNum++;
}

